package com.example.ai.controller;

import com.example.ai.dto.wrongquestion.ReviewNoteRequest;
import com.example.ai.dto.wrongquestion.WrongQuestionRequest;
import com.example.ai.dto.wrongquestion.WrongQuestionResponse;
import com.example.ai.service.WrongQuestionService;
import com.example.ai.dto.review.ReviewHistoryResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/wrong-questions")
@RequiredArgsConstructor
@CrossOrigin(origins = "*", maxAge = 3600)
public class WrongQuestionController {

    private final WrongQuestionService wrongQuestionService;

    @PostMapping
    public ResponseEntity<WrongQuestionResponse> addToWrongQuestion(
            Authentication authentication,
            @Valid @RequestBody WrongQuestionRequest request) {
        WrongQuestionResponse response = wrongQuestionService.addToWrongQuestion(
                authentication.getName(), request);
        return ResponseEntity.ok(response);
    }

    @PutMapping("/{id}")
    public ResponseEntity<WrongQuestionResponse> updateWrongQuestion(
            @PathVariable Long id,
            @Valid @RequestBody WrongQuestionRequest request
    ) {
        return ResponseEntity.ok(wrongQuestionService.updateWrongQuestion(id, request));
    }

    @GetMapping
    public ResponseEntity<Page<WrongQuestionResponse>> getWrongQuestions(
            Authentication authentication,
            Pageable pageable) {
        Page<WrongQuestionResponse> response = wrongQuestionService.getWrongQuestions(
                authentication.getName(), pageable);
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteWrongQuestion(@PathVariable Long id) {
        wrongQuestionService.deleteWrongQuestion(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/review")
    public ResponseEntity<WrongQuestionResponse> addReviewNote(
            @PathVariable Long id,
            @Valid @RequestBody ReviewNoteRequest request) {
        request.setWrongQuestionId(id);  // 确保使用路径中的ID
        return ResponseEntity.ok(wrongQuestionService.addReviewNote(request));
    }

    @GetMapping("/{id}/review-history")
    public ResponseEntity<Page<ReviewHistoryResponse>> getReviewHistory(
            @PathVariable Long id,
            Pageable pageable) {
        return ResponseEntity.ok(wrongQuestionService.getReviewHistory(id, pageable));
    }
} 